package com.jz.community.moduleshoppingguide.nearshop.ui.view;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.NearShopGoodsRight;
import com.jz.community.moduleshoppingguide.nearshop.ui.adapter.StandardAdapter;

public class StandardPop extends BottomSheetDialogFragment {


    ImageView stairGoodsStandardImg;
    TextView stairGoodsStandardTitle;
    TextView stairGoodsStandardPrice;
    TextView stairGoodsStandardCostPrice;
    RecyclerView stairGoodsStandardRv;
    Button stairGoodsStandardOk;
    ImageView stairGoodsStandardClose;

    NearShopGoodsRight nearShopGoodsRight;
    IGetSkuBean getSkuBean;
    StandardAdapter standardAdapter;
    private String skuName;
    private String skuId;
    private String price;
    private String sId;

    public static StandardPop newInstance(NearShopGoodsRight nearShopGoodsRight) {
        StandardPop standardPop = new StandardPop();
        Bundle bundle = new Bundle();
        bundle.putSerializable("nearShopGoodsRight", nearShopGoodsRight);
        standardPop.setArguments(bundle);
        return standardPop;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.module_shoppingguide_layout_stair_goods_standard, container);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        stairGoodsStandardImg = view.findViewById(R.id.stair_goods_standard_img);
        stairGoodsStandardTitle = view.findViewById(R.id.stair_goods_standard_title);
        stairGoodsStandardPrice = view.findViewById(R.id.stair_goods_standard_price);
        stairGoodsStandardCostPrice = view.findViewById(R.id.stair_goods_standard_cost_price);
        stairGoodsStandardRv = view.findViewById(R.id.stair_goods_standard_rv);
        stairGoodsStandardOk = view.findViewById(R.id.stair_goods_standard_ok);
        stairGoodsStandardClose = view.findViewById(R.id.stair_goods_standard_close);
    }


    private void initData() {
        nearShopGoodsRight = (NearShopGoodsRight) getArguments().getSerializable("nearShopGoodsRight");
        stairGoodsStandardOk.setEnabled(false);
        stairGoodsStandardTitle.setText(nearShopGoodsRight.getTitle());
        GlideApp.with(getActivity())
                .load(nearShopGoodsRight.getImage())
                .placeholder(R.mipmap.image_default)
                .error(R.mipmap.image_default)
                .into(stairGoodsStandardImg);
        stairGoodsStandardRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        if (nearShopGoodsRight.getSkuProperties() != null && nearShopGoodsRight.getSkuProperties().size() > 0) {
            standardAdapter = new StandardAdapter(R.layout.module_shoppingguide_item_sku, nearShopGoodsRight.getSkuProperties());
            stairGoodsStandardRv.setAdapter(standardAdapter);
            standardAdapter.setCstandard(() -> {
                setButtonChick();
                setTextPrice();
            });
        }

        stairGoodsStandardOk.setOnClickListener(v -> {
            if (getSkuBean != null) {
                getSkuBean.currentSkuBean(skuName, skuId, price, sId);
            }
            dismiss();
        });
        stairGoodsStandardClose.setOnClickListener(v -> dismiss());
        setTextPrice();
        setButtonChick();
        setDefault();
    }

    private void setDefault() {
        if (!chooseOK()) {
            if (standardAdapter.getData().size() == 1) {
                standardAdapter.getData().get(0).setChickedOk(true);
                standardAdapter.getData().get(0).getPropertyValues().get(0).setChick(true);
                standardAdapter.getData().get(0).setCurrentChooseSkuId(standardAdapter.getData().get(0).getId() + "|" + standardAdapter.getData().get(0).getPropertyValues().get(0).getId());
            } else {
                for (int i = 0; i < standardAdapter.getData().size(); i++) {
                    standardAdapter.getData().get(i).setChickedOk(true);
                    standardAdapter.getData().get(i).getPropertyValues().get(0).setChick(true);
                    standardAdapter.getData().get(i).setCurrentChooseSkuId(standardAdapter.getData().get(i).getId() + "|" + standardAdapter.getData().get(i).getPropertyValues().get(0).getId());
                }
            }
            standardAdapter.notifyDataSetChanged();
            setTextPrice();
            setButtonChick();
        }
    }


    private void setButtonChick() {
        if (chooseOK()) {
            stairGoodsStandardOk.setEnabled(true);
            stairGoodsStandardOk.setBackgroundColor(Color.parseColor("#f2383b"));
        } else {
            stairGoodsStandardOk.setEnabled(false);
            stairGoodsStandardOk.setBackgroundColor(Color.parseColor("#A9A9A9"));
        }
    }

    private boolean chooseOK() {
        for (int i = 0; i < standardAdapter.getData().size(); i++) {
            if (standardAdapter.getData().get(i).isChickedOk()) {
                return true;
            }
        }
        return false;
    }

    private void setTextPrice() {
        if (chooseOK()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < standardAdapter.getData().size(); i++) {
                if (!TextUtils.isEmpty(standardAdapter.getData().get(i).getCurrentChooseSkuId())) {
                    if (sb.length() > 0) {
                        sb.append("|");
                    }
                    sb.append(standardAdapter.getData().get(i).getCurrentChooseSkuId());
                } else {
                    continue;
                }
            }
            String sku = sb.toString();
            for (int i = 0; i < nearShopGoodsRight.getSkuInfos().size(); i++) {
                if (sku.trim().equals(nearShopGoodsRight.getSkuInfos().get(i).getSpidStr())) {
                    if (!TextUtils.isEmpty(nearShopGoodsRight.getSkuInfos().get(i).getDiscountPrice())) {
                        stairGoodsStandardPrice.setText(nearShopGoodsRight.getSkuInfos().get(i).getDiscountPrice());
                        RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(nearShopGoodsRight.getSkuInfos().get(i).getPrice()))).setStrikethrough().into(stairGoodsStandardCostPrice);
                        price = nearShopGoodsRight.getSkuInfos().get(i).getDiscountPrice();
                    } else {
                        stairGoodsStandardPrice.setText(nearShopGoodsRight.getSkuInfos().get(i).getPrice());
                        price = nearShopGoodsRight.getSkuInfos().get(i).getPrice();
                    }
                    skuName = nearShopGoodsRight.getSkuInfos().get(i).getSpStr();
                    skuId = nearShopGoodsRight.getSkuInfos().get(i).getSpidStr();
                    sId = nearShopGoodsRight.getSkuInfos().get(i).getId();
                    break;
                }
            }
        }
    }


    public interface IGetSkuBean {
        void currentSkuBean(String skName, String skId, String price, String sId);
    }

    public void setGetSkuBean(IGetSkuBean getSkuBean) {
        this.getSkuBean = getSkuBean;
    }
}
